// 商品列表
const api = require('../../../utils/api') // 引入API
const {myRequest} = require('../../../utils/myRequest') // 请求函数
Page({
  data: {
    filterCategoryList: [],// 分类名称
    goodsList: [],// 分类详情列表
    page: 1,  // 当前页数第一页
    switchId: '', // 选择点击id
    categoryId: '' // 
  },
  onLoad: function (options) {
    let categoryId = options.id;
    this.setData({
      categoryId: categoryId
    })
    this.goodsList(1, categoryId)
  },
  // 请求函数
  goodsList(page,categoryId) {
    myRequest({
      url: api.goodsList,
      data: {
        categoryId: categoryId,
        page: page,
        size: 10
      },
      success: res => {
        let filterCategoryList = res.data.data.filterCategory // 分类名称
        let goodsList = res.data.data.goodsList      // 分类详情列表
        this.setData({
          filterCategoryList: filterCategoryList,
          goodsList: goodsList
        })
      }
    })
  },
  // 切换选择
  selectItem(e) {
    let id = e.currentTarget.dataset.id;
    this.setData({
      switchId: id
    })
    this.data.page = 1;
    this.goodsList(1, id)
  },  
  // 下拉加载更多
  onReachBottom: function () {
    wx.showLoading({
      title: '加载中...',
      duration: 1500
    });
    const { categoryId,goodsList } = this.data
    this.data.page += 1;
    myRequest({
      url: api.goodsList,
      data: {
        categoryId: categoryId,
        page: this.data.page,
        size: 10
      },
      success: res => {
        if (res.data.data.goodsList.length !== 0) {
          setTimeout( () => {
             this.setData({
              goodsList: goodsList.concat( res.data.data.goodsList )
            })
          }, 1500)
        } else {
          wx.showToast({
            title: '到底啦~',
          });
          return false;
        }
      }
    })
  },
  onShareAppMessage: function () {

  }
})